package Twitter
{
	
	import flash.data.SQLResult;
	
	import mx.collections.ArrayCollection;
	
	public class t_database_actions
	{
		import flash.data.SQLConnection;
		import flash.data.SQLStatement;
		import flash.events.SQLErrorEvent;
		import flash.events.SQLEvent;
		import flash.filesystem.File;
		
		import mx.controls.Alert;
		import mx.managers.CursorManager;
		
		import mx.core.Application
		
		import Twitter.user_stored_keywords;
		import Twitter.Groups;
		import Twitter.GroupMembers;
		
		[Bindable]
		public var dbFile:File = File.applicationStorageDirectory.resolvePath("rt.db");
		
		[Bindable]
		private var dbStatement:SQLStatement;
		
		[Bindable]
		public var conn:SQLConnection;
		
		[Bindable]
		public var cr:CursorManager;
		
		[Bindable]
		public var _arrKeywords:ArrayCollection;
		
		public var _reset_database:Boolean = false;
		
		public var _array_db_keywords:Array;
		
		
		
		public function t_database_actions()
		{
		}
		
		//create database tables - keywords & groups
		
		/////////////////////
		//DATABASE FUNCTIONS
		/////////////////////
		
		public function drop_table():void
		{
			drop_tbl_keywords();
			drop_tbl_groups();
			drop_tbl_members();
			drop_timed_tweets();
			
		}
		
		private function drop_tbl_keywords():void
		{
			var sqlQuery:String = "DROP TABLE IF EXISTS tbl_keywords"
			dbStatement = new SQLStatement();
			dbStatement.sqlConnection = conn;
			dbStatement.text = sqlQuery;
			dbStatement.addEventListener(SQLEvent.RESULT, onDBDropTables_keywords);
			dbStatement.addEventListener(SQLErrorEvent.ERROR, onDBDropTablesError);
			dbStatement.execute();
		}
		
		private function drop_tbl_groups():void
		{
			var sqlQuery:String = "DROP TABLE IF EXISTS tbl_groups"
			dbStatement = new SQLStatement();
			dbStatement.sqlConnection = conn;
			dbStatement.text = sqlQuery;
			dbStatement.addEventListener(SQLEvent.RESULT, onDBDropTables_groups);
			dbStatement.addEventListener(SQLErrorEvent.ERROR, onDBDropTablesError);
			dbStatement.execute();
		}
		
		private function drop_tbl_members():void
		{
			var sqlQuery:String = "DROP TABLE IF EXISTS tbl_group_members"
			dbStatement = new SQLStatement();
			dbStatement.sqlConnection = conn;
			dbStatement.text = sqlQuery;
			dbStatement.addEventListener(SQLEvent.RESULT, onDBDropTables_group_members);
			dbStatement.addEventListener(SQLErrorEvent.ERROR, onDBDropTablesError);
			dbStatement.execute();
		}
		
		private function drop_timed_tweets():void
		{
			var sqlQuery:String = "DROP TABLE IF EXISTS tbl_timed_tweets"
			dbStatement = new SQLStatement();
			dbStatement.sqlConnection = conn;
			dbStatement.text = sqlQuery;
			dbStatement.addEventListener(SQLEvent.RESULT, onDBDropTables_timed_tweets);
			dbStatement.addEventListener(SQLErrorEvent.ERROR, onDBDropTablesError);
			dbStatement.execute();
		}
		
		private function onDBDropTables_timed_tweets(event:SQLEvent):void
		{
			
			//create_tables();
			create_tbl_timed_tweets();
			
		}
		
		private function onDBDropTables_keywords(event:SQLEvent):void
		{
			
			//create_tables();
			create_tbl_keywords();
			
		}
		
		private function onDBDropTables_groups(event:SQLEvent):void
		{
			
			//create_tables();
			create_tbl_groups();
			
		}
		
		private function onDBDropTables_group_members(event:SQLEvent):void
		{
			
			//create_tables();
			create_tbl_group_members();
			
		}
		
		private function onDBDropTablesError(event:SQLErrorEvent):void
		{
			Alert.show("Failed to Drop Tables");	
		
		}
		
		public function openDataBase():void
		{
			dbFile= File.applicationStorageDirectory.resolvePath("rt.db");
			conn = new SQLConnection();
			conn.addEventListener(SQLEvent.OPEN, openHandler);

			conn.addEventListener(SQLErrorEvent.ERROR, errorHandler);
			conn.open(dbFile);
			
			
		}
		
		private function openHandler(event:SQLEvent):void
		{
		    //Alert.show("the database opened successfully");
		    if (Application.application._reset_database==true)
			{		drop_table();
			}
			else
			{
		    		create_tables();
		 	}


		}
		
		private function errorHandler(event:SQLErrorEvent):void
		{
		    trace("Error message:", event.error.message);
		}
		
		private function create_tables():void
		{
			//DROP TABLE IF EXISTS tbl_status ; 
			create_tbl_keywords();
			create_tbl_groups();
			create_tbl_group_members();
			create_tbl_timed_tweets()
			
			
		}
		
		private function create_tbl_timed_tweets():void
		{
			//DROP TABLE IF EXISTS tbl_status ; 
			var sqlQuery:String = "CREATE TABLE IF NOT EXISTS tbl_timed_tweets (" +
                   "id TEXT," +
			       "message TEXT," + 
			       "time TEXT, username TEXT, sending INTEGER " +
		           "); ";
		           
		           
			dbStatement = new SQLStatement();
			dbStatement.sqlConnection = conn;
			dbStatement.text = sqlQuery;
			dbStatement.addEventListener(SQLEvent.RESULT, onDBStatementCreateResult);
			dbStatement.addEventListener(SQLErrorEvent.ERROR, onDBCreateError);
			dbStatement.execute();
			
		}
		
		private function create_tbl_keywords():void
		{
			//DROP TABLE IF EXISTS tbl_status ; 
			var sqlQuery:String = "CREATE TABLE IF NOT EXISTS tbl_keywords (" +
                   "keyword TEXT," +
			       "group_id TEXT," + 
			       "username TEXT " +
		           "); ";
		           
		           
			dbStatement = new SQLStatement();
			dbStatement.sqlConnection = conn;
			dbStatement.text = sqlQuery;
			dbStatement.addEventListener(SQLEvent.RESULT, onDBStatementCreateResult);
			dbStatement.addEventListener(SQLErrorEvent.ERROR, onDBCreateError);
			dbStatement.execute();
			
		}
		

		
		private function create_tbl_groups():void
		{
			//DROP TABLE IF EXISTS tbl_status ; 
			var sqlQuery:String = "CREATE TABLE IF NOT EXISTS tbl_groups(" +
		           "group_id TEXT," +
		           "username TEXT, "+
		           "is_public TEXT)";
		           
		           
			dbStatement = new SQLStatement();
			dbStatement.sqlConnection = conn;
			dbStatement.text = sqlQuery;
			dbStatement.addEventListener(SQLEvent.RESULT, onDBStatementCreateResult);
			dbStatement.addEventListener(SQLErrorEvent.ERROR, onDBCreateError);
			dbStatement.execute();
		}
		

		
		private function create_tbl_group_members():void
		{
			//DROP TABLE IF EXISTS tbl_status ; 
			var sqlQuery:String = "CREATE TABLE IF NOT EXISTS tbl_group_members (" +
		           "user_id TEXT," + 
		           "group_id TEXT, username TEXT)";
		           
		           
			dbStatement = new SQLStatement();
			dbStatement.sqlConnection = conn;
			dbStatement.text = sqlQuery;
			dbStatement.addEventListener(SQLEvent.RESULT, onDBStatementCreateResult);
			dbStatement.addEventListener(SQLErrorEvent.ERROR, onDBCreateError);
			dbStatement.execute();
		}
		
		public function add_column_time_tweeter():void
		{
			try
			
			{
			var sqlQuery:String = "ALTER TABLE tbl_timed_tweets ADD COLUMN sending INTEGER";
		           
		           
			dbStatement = new SQLStatement();
			dbStatement.sqlConnection = conn;
			dbStatement.text = sqlQuery;
			//dbStatement.addEventListener(SQLEvent.RESULT, onDBStatementCreateResult);
			//dbStatement.addEventListener(SQLErrorEvent.ERROR, onDBCreateError);
			dbStatement.execute();
			}
			catch(err:ReferenceError)
			{
				
				
			}
			
			
			
		}
		
		private function onDBStatementCreateResult(event:SQLEvent):void
		{
			CursorManager.removeBusyCursor();
			//Alert.show("Tabels Created");
			//dbStatement.removeEventListener(SQLEvent.RESULT, onDBStatementCreateResult);
			//dbStatement.removeEventListener(SQLErrorEvent.ERROR, onDBCreateError);
			
		}
		
		private function onDBCreateError(event:SQLErrorEvent):void
		{
			Alert.show("Database Error");

		}
		
		public function insertKeywords(keyword:String, username:String):void
		{
			/*var key:user_stored_keywords = new user_stored_keywords();
			
			key._key_group = "0";
			key._keywords = keyword;
			key._username = username;*/
			
			var sqlInsert:String = "insert into tbl_keywords values ('"+keyword+"','0','"+username+"')";
			dbStatement.text = sqlInsert;
    		dbStatement.removeEventListener(SQLEvent.RESULT, onDBStatementCreateResult);
    		dbStatement.addEventListener(SQLEvent.RESULT, onDBStatementInsertKeywordResult);
    		dbStatement.addEventListener(SQLErrorEvent.ERROR, onDBStatementInsertKeywordResultError);
    		dbStatement.execute();
			
		}
		
		public function deleteKeywords(keyword:String, username:String):void
		{
			/*var key:user_stored_keywords = new user_stored_keywords();
			
			key._key_group = "0";
			key._keywords = keyword;
			key._username = username;*/
			
			var sqlInsert:String = "delete from tbl_keywords WHERE keyword = '"+ keyword +"' AND username = '"+ username+"'";
			dbStatement.text = sqlInsert;
    		dbStatement.removeEventListener(SQLEvent.RESULT, onDBStatementCreateResult);
    		dbStatement.addEventListener(SQLEvent.RESULT, onDBStatementInsertKeywordResult);
    		dbStatement.addEventListener(SQLErrorEvent.ERROR, onDBStatementInsertKeywordResultError);
    		dbStatement.execute();
			
		}
		
		public function onDBStatementInsertKeywordResult( event:SQLEvent):void
		{
			//Alert.show("keyword added");
			
			dbStatement.removeEventListener(SQLEvent.RESULT, onDBStatementInsertKeywordResult);
    		dbStatement.removeEventListener(SQLErrorEvent.ERROR, onDBStatementInsertKeywordResultError);
			
			//rebind array
		}
		
		public function onDBStatementInsertKeywordResultError(event:SQLErrorEvent):void
		{
			//Alert.show("failed to add keyword");
			
			dbStatement.removeEventListener(SQLEvent.RESULT, onDBStatementInsertKeywordResult);
    		dbStatement.removeEventListener(SQLErrorEvent.ERROR, onDBStatementInsertKeywordResultError);
		}
		
		public function getKeywords(username:String):void
		{
			var sqlQuery:String = "SELECT * FROM tbl_keywords WHERE username = '"+username+"' ORDER BY keyword";        
		           
			dbStatement = new SQLStatement();
			dbStatement.sqlConnection = conn;
			dbStatement.itemClass = user_stored_keywords;
			dbStatement.text = sqlQuery;
			dbStatement.addEventListener(SQLEvent.RESULT, bindKeywords);
			dbStatement.execute();
			
			
			
			
		}
		private function bindKeywords(event:SQLEvent):void
		{
			var result:SQLResult = dbStatement.getResult();
    		if (result != null)
    		{
        		_array_db_keywords = result.data;
        		//Alert.show("Keyword Count = " + _array_db_keywords.length.toString());
        		
        		//Application.application.listKeywords == new list_keywords();
    		}
			dbStatement.removeEventListener(SQLEvent.RESULT, bindKeywords);
		}
		
		/////////////////////
		// GROUPS
		/////////////////////
		
		public var _ins_username:String = "";
		public var _in_group_id:String = "";
		
		public function insertGroup(group_id:String, username:String):Boolean
		{
			var inserted:Boolean = false;
			/*
		CREATE TABLE IF NOT EXISTS tbl_groups(" +
		           "group_id TEXT," +
		           "username TEXT, "+
		           "is_public TEXT 
		*/
			_ins_username = username;
			_in_group_id = group_id;
			
			dbStatement = new SQLStatement();
			dbStatement.sqlConnection = conn;
			
			var sqlSelect:String = "SELECT * FROM tbl_groups WHERE group_id = '"+group_id+"' AND username = '"+username+"'"; //group_id TEXT," +"username TEXT, "+"is_public TEXT)"
			dbStatement.text = sqlSelect;
    		dbStatement.removeEventListener(SQLEvent.RESULT, onDBStatementCreateResult);
    		//dbStatement.addEventListener(SQLEvent.RESULT, insert_tbl_group);
    		dbStatement.addEventListener(SQLErrorEvent.ERROR, onDBStatementInsertKeywordResultError);
    		
    		dbStatement.itemClass = Groups;
    		dbStatement.execute();
    		
    		//Alert.show(sqlSelect);
    		
    		var result:SQLResult = dbStatement.getResult();
    		if (result.data==null)
    		{
    				
        			var sqlInsert:String = "insert into tbl_groups values ('"+_in_group_id+"','"+_ins_username+"', '0')";
					dbStatement.text = sqlInsert;
    				//dbStatement.removeEventListener(SQLEvent.RESULT, insert_tbl_group);
    				//dbStatement.addEventListener(SQLErrorEvent.ERROR, onDBStatementInsertKeywordResultError);
    				dbStatement.execute();
    				//Application.application.groupactions.bind_group_titles();
    			//Alert.show(sqlInsert);
    				inserted = true;
     		 }
    		
    		return inserted;
			
		}
		
		/*private function insert_tbl_group(event:SQLEvent):void
		{
			var result:SQLResult = dbStatement.getResult();
    		if (result==null)
    		{
    			
        			var sqlInsert:String = "insert into tbl_groups values ('"+_in_group_id+"','"+_ins_username+"', '0')";
					dbStatement.text = sqlInsert;
    				dbStatement.removeEventListener(SQLEvent.RESULT, insert_tbl_group);
    				dbStatement.addEventListener(SQLErrorEvent.ERROR, onDBStatementInsertKeywordResultError);
    				dbStatement.execute();
    				Application.application.groupactions.bind_group_titles();
    			Alert.show(sqlInsert);
     		 }
  		}*/
		
		public function getGroup(username:String):void
		{
			var sqlQuery:String = "SELECT * FROM tbl_groups WHERE username = '"+username+"' ORDER BY group_id";        
		           
			dbStatement = new SQLStatement();
			dbStatement.sqlConnection = conn;
			dbStatement.itemClass = Groups;
			dbStatement.text = sqlQuery;
			dbStatement.addEventListener(SQLEvent.RESULT, bindGroups);
			dbStatement.execute();		
			
		}
		
		public var _array_db_groups:Array;
		
		private function bindGroups(event:SQLEvent):void
		{
			var result:SQLResult = dbStatement.getResult();
    		if (result != null)
    		{
        		_array_db_groups = result.data;
        		//Alert.show("Keyword Count = " + _array_db_keywords.length.toString());
        		
        		//Application.application.listKeywords == new list_keywords();
    		}
			dbStatement.removeEventListener(SQLEvent.RESULT, bindKeywords);
		}
		
		public function check_exists_GroupMember(user_id: String, group_id:String, username:String):Boolean
		{
			dbStatement = new SQLStatement();
			dbStatement.sqlConnection = conn;
			
			var sqlSelect:String = "SELECT * FROM tbl_group_members WHERE group_id = '"+group_id+"' AND username = '"+username+"' AND user_id='"+user_id+"'";
			dbStatement.text = sqlSelect;
			dbStatement.execute();
			
			var result:SQLResult = dbStatement.getResult();
			
			if (result.data!=null)
			{
				return true;
				
			}
			else
			{
				return false;
				
			}
			
			
			
		}
		
		
		public function insertGroupMember(user_id: String, group_id:String, username:String):void
		{
			/*
		CREATE TABLE IF NOT EXISTS tbl_group_members (" +
		           "user_id TEXT," + 
		           "group_id TEXT, username TEXT
		 	*/
			
			var sqlInsert:String = "insert into tbl_group_members values ('"+user_id+"', '"+group_id+"','"+username+"')";
			dbStatement.text = sqlInsert;
    		dbStatement.removeEventListener(SQLEvent.RESULT, onDBStatementCreateResult);
    		dbStatement.addEventListener(SQLEvent.RESULT, onDBStatementInsertKeywordResult);
    		dbStatement.addEventListener(SQLErrorEvent.ERROR, onDBStatementInsertKeywordResultError);
    		dbStatement.execute();
			
		}
		
		
		
		public function getGroupMember(group_id:String, username:String):void
		{
			var sqlQuery:String = "SELECT * FROM tbl_group_members WHERE username = '"+username+"' AND group_id = '"+group_id+"' ORDER BY user_id";        
		           
			dbStatement = new SQLStatement();
			dbStatement.sqlConnection = conn;
			dbStatement.itemClass = GroupMembers;
			dbStatement.text = sqlQuery;
			dbStatement.addEventListener(SQLEvent.RESULT, bindGroupMembers);
			dbStatement.execute();		
			
		}
		
		public function get_stored_user_in_groups(user_id: String, username:String):Array
		{
			var arr_group:Array= new Array();
			/*
		CREATE TABLE IF NOT EXISTS tbl_group_members (" +
		           "user_id TEXT," + 
		           "group_id TEXT, username TEXT
		 	*/
			
			var sqlQuery:String = "select * from tbl_group_members where user_id='"+user_id+"' and username='"+username+"'";
			dbStatement = new SQLStatement();
			dbStatement.sqlConnection = conn;
			dbStatement.itemClass = GroupMembers;
			dbStatement.text = sqlQuery;
			//dbStatement.addEventListener(SQLEvent.RESULT, bindUerGroups);
			dbStatement.execute();	
			
			var result:SQLResult = dbStatement.getResult();
			
			if (result != null)
	    		{
	        		arr_group = result.data;
	        		
	        		
	        		
	        		//Application.application.listKeywords == new list_keywords();
	    		}
			
			try
			{
	    		
   			}
   			catch(error:Error)
   			{
   				
   			}
			
			return arr_group;
			
		}
		
		
		public var _array_db_group_members:Array;
		
		
		private function bindGroupMembers(event:SQLEvent):void
		{
			var result:SQLResult = dbStatement.getResult();
    		if (result != null)
    		{
        		_array_db_group_members = result.data;
        		//Alert.show("Keyword Count = " + _array_db_keywords.length.toString());
        		
        		//Application.application.listKeywords == new list_keywords();
    		}
			dbStatement.removeEventListener(SQLEvent.RESULT, bindKeywords);
		}
		
		
		
		public function getGlobalGroupMember(username:String):void
		{
			var sqlQuery:String = "SELECT * FROM tbl_group_members WHERE username = '"+username+"'";        
		           
			dbStatement = new SQLStatement();
			dbStatement.sqlConnection = conn;
			dbStatement.itemClass = GroupMembers;
			dbStatement.text = sqlQuery;
			dbStatement.addEventListener(SQLEvent.RESULT, bindGlobalGroupMembers);
			dbStatement.execute();		
			
		}
		
		public var _array_db_global_group_members:Array;
		
		private function bindGlobalGroupMembers(event:SQLEvent):void
		{
			_array_db_global_group_members = [];
			
			var result:SQLResult = dbStatement.getResult();
    		if (result != null)
    		{
        		_array_db_global_group_members = result.data;
        		//
        		//Alert.show("_array_db_global_group_members = " + _array_db_global_group_members.length.toString());
        		
        		//Application.application.listKeywords == new list_keywords();
    		}
			dbStatement.removeEventListener(SQLEvent.RESULT, bindKeywords);
		}
		
		public function deleteGroupMember(group_id:String, user_id:String, username:String):void
		{
			var sqlQuery:String = "DELETE FROM tbl_group_members WHERE user_id = '"+user_id+"' AND group_id = '"+group_id+"' AND username = '"+username+"'";        
		           
			dbStatement = new SQLStatement();
			dbStatement.sqlConnection = conn;
			dbStatement.text = sqlQuery;
			dbStatement.addEventListener(SQLEvent.RESULT, bindGroupMembers);
			//dbStatement.addEventListener(SQLErrorEvent.ERROR, onDBStatementdeleteGroupMemberResultError);
			dbStatement.execute();		
			
		}
		
		public function deleteAllGroupMembers(group_id:String, username:String):void
		{
			var sqlQuery:String = "DELETE FROM tbl_group_members WHERE  group_id = '"+group_id+"' AND username = '"+username+"'";        
		           
			dbStatement = new SQLStatement();
			dbStatement.sqlConnection = conn;
			dbStatement.text = sqlQuery;
			//dbStatement.addEventListener(SQLEvent.RESULT, bindGroupMembers);
			//dbStatement.addEventListener(SQLErrorEvent.ERROR, onDBStatementdeleteGroupMemberResultError);
			dbStatement.execute();		
			
		}
		
		public function deleteGroup(group_id:String, username:String):void
		{
			var sqlQuery:String = "DELETE FROM tbl_groups WHERE group_id = '"+group_id+"' AND username = '"+username+"'";        
		           
			dbStatement = new SQLStatement();
			dbStatement.sqlConnection = conn;
			dbStatement.text = sqlQuery;
			dbStatement.addEventListener(SQLEvent.RESULT, bindGroupMembers);
			//dbStatement.addEventListener(SQLErrorEvent.ERROR, onDBStatementdeleteGroupMemberResultError);
			dbStatement.execute();		
			
		}
		
		/// Scheduled
		
		public function get_scheduled_tweets(username:String):void
		{
			var sqlQuery:String = "SELECT * FROM tbl_timed_tweets WHERE username = '"+username+"'";        
		           
			dbStatement = new SQLStatement();
			dbStatement.sqlConnection = conn;
			//dbStatement.itemClass = ScheduledMessage;
			dbStatement.text = sqlQuery;
			dbStatement.addEventListener(SQLEvent.RESULT, bindScheduledTweets);
			dbStatement.execute();		
			
		}
		
		private function bindScheduledTweets(event:SQLEvent):void
		{
			//Application.application.ScheduledMessage = [];
			
			Application.application.ScheduledMessage  = new ArrayCollection();
			
			var result:SQLResult = dbStatement.getResult();
    		if (result.data != null)
    		{
    			var numResults:int = result.data.length;
    			for (var i:int = 0; i < numResults; i++)
			    {
			        var row:Object = result.data[i];
			        var o:ScheduledMessage = new ScheduledMessage();
			        o.id = row.id;
			        o.message = row.message;
			        o.time = new Date(row.time);
			        o.username = row.username;
			        o.sending = false;
			        
			        Application.application.ScheduledMessage.addItem(o);
			    }
    			
        		//_array_scheduled_tweet = result.data.username;
        		
				try
				{
        			Application.application._scheduled_tweet_count = Application.application.ScheduledMessage.length;
    			}
    			catch (err:Error)
    			{
    				
    			}
    			
    			//Alert.show(Application.application._scheduled_tweet_count);
    		}
			dbStatement.removeEventListener(SQLEvent.RESULT, bindKeywords);
			
			
			CursorManager.removeBusyCursor();
		}
		
		public function insert_scheduled_tweet(username:String, message:String, time:String):void
		{
			/*
		CREATE TABLE IF NOT EXISTS tbl_group_members (" +
		           "user_id TEXT," + 
		           "group_id TEXT, username TEXT
		 	*/
		 	
		 	var id:int = Application.application._scheduled_tweet_count + 1;
		 	
		 	var timestamp:Date = new Date(); 
		 	
		 	//Alert.show("message added to schedule");
			
			var sqlInsert:String = "insert into tbl_timed_tweets values (:id, :message, :time, :username)"; //"+id.toString()+",'"+message+"','"+time+"','"+username+"'
			dbStatement.text = sqlInsert;
			dbStatement.parameters[":id"] = timestamp.toString();
			dbStatement.parameters[":message"] = message;
			dbStatement.parameters[":time"] = time;
			dbStatement.parameters[":username"] = username;
    		dbStatement.removeEventListener(SQLEvent.RESULT, onDBStatementCreateResult);
    		dbStatement.addEventListener(SQLEvent.RESULT, call_to_scheduled_tweet_bind);
    		dbStatement.addEventListener(SQLErrorEvent.ERROR, onDBStatementInsertKeywordResultError);
    		dbStatement.execute();
			
		}
		
		private function call_to_scheduled_tweet_bind(event:SQLEvent):void
		{
			
			get_scheduled_tweets(Application.application._username);
			
			
		}
		
		public function update_scheduled_tweet(id:String, username:String, message:String, time:String):void
		{
			/*
		CREATE TABLE IF NOT EXISTS tbl_group_members (" +
		           "user_id TEXT," + 
		           "group_id TEXT, username TEXT
		 	*/
		 	
		 	//var id:int = Application.application._scheduled_tweet_count + 1;
		 	
		 	var timestamp:Date = new Date(); 
		 	
		 	//Alert.show(timestamp.toString());
			
			var sqlUpdate:String = "update tbl_timed_tweets set time=:time, message=:message where id=:id"; //"+id.toString()+",'"+message+"','"+time+"','"+username+"'
			dbStatement.text = sqlUpdate;
			dbStatement.parameters[":id"] = id
			dbStatement.parameters[":message"] = message;
			dbStatement.parameters[":time"] = time;
			//dbStatement.parameters[":username"] = username;
			
			//Alert.show(sqlUpdate);
			
			dbStatement.addEventListener(SQLEvent.RESULT, call_to_scheduled_tweet_bind);
    		//dbStatement.removeEventListener(SQLEvent.RESULT, onDBStatementCreateResult);
    		//dbStatement.addEventListener(SQLEvent.RESULT, onDBStatementInsert_scheduled_tweet);
    		dbStatement.addEventListener(SQLErrorEvent.ERROR, onDBStatement_update_scheduled_tweetError);
    		dbStatement.execute();
			
		}
		
		public function onDBStatement_update_scheduled_tweetError(event:SQLErrorEvent):void
		{
			Alert.show("failed to update schedule");
			
			dbStatement.removeEventListener(SQLEvent.RESULT, onDBStatementInsertKeywordResult);
    		dbStatement.removeEventListener(SQLErrorEvent.ERROR, onDBStatementInsertKeywordResultError);
		}
		
		public function onDBStatementInsert_scheduled_tweet( event:SQLEvent):void
		{
			//Alert.show("schedule added");
			
			dbStatement.removeEventListener(SQLEvent.RESULT, onDBStatementInsertKeywordResult);
    		dbStatement.removeEventListener(SQLErrorEvent.ERROR, onDBStatementInsertKeywordResultError);
			
		}
		
		public function deleteScheduledTweet(id:String):void
		{
			var sqlQuery:String = "DELETE FROM tbl_timed_tweets WHERE id = '"+id+"'";        
		           
			dbStatement = new SQLStatement();
			dbStatement.sqlConnection = conn;
			dbStatement.text = sqlQuery;
			dbStatement.addEventListener(SQLEvent.RESULT, call_to_scheduled_tweet_bind);
			//dbStatement.addEventListener(SQLErrorEvent.ERROR, onDBStatementdeleteGroupMemberResultError);
			dbStatement.execute();	
			
			//Get schedueled Tweets
			Application.application.t_db.get_scheduled_tweets(Application.application._username);
			
		}

		
		/////////////////////
		//END DATABASE
		/////////////////////

	}
}